import { defineStore } from "pinia";
import { ElMessage } from "element-plus";
import { ref, reactive } from "vue"
import mittBus from '@/utils/mitt';
import { useEnumStore } from "@/store/enumeration"
import { voucherSaveOrUpdatePagePost, voucherSaveOrUpdateDeleteDel, equityDetailGet, equityListDataByIdGet ,voucherSaveOrUpdateDetailGet} from "@/api/equity"
import useInstance from '@/hooks/useInstance';
export const virtualMemberStore = defineStore("virtualMemberStoreId", () => {
    const { global } = useInstance();
    const detailRef = ref<{ show: (title: string, row?: any) => void }>()
    const addAndEditRef = ref<{ show: (type: string, row?: any) => void }>()
    const inventoryRef = ref<{ show: (type: string, row?: any) => void }>()



    const { reductionVoucherType, expiryPeriodType, isShow, schemeConfigletailsType, virtualType } = useEnumStore()

    const detailData = ref([
        {
            prop: 'supplierCode',
            name: '供应商编码',
            type: 'text',
            value: null
        },
        {
            prop: 'equityVoucherType',
            name: '权益券类型',
            type: 'text',
            value: null
        },
        {
            prop: 'equityVoucherCode',
            name: '权益券编码',
            type: 'text',
            value: null
        },
        {
            prop: 'equityVoucherName',
            name: '权益券名称',
            type: 'text',
            value: null
        },
        {
            prop: 'supplierEquityVoucherCode',
            name: '供应商权益编码',
            type: 'text',
            value: null
        },
        {
            prop: 'receiveType',
            name: '领取类型',
            type: 'text',
            value: null
        },
        {
            prop: 'receiveValue',
            name: '领取值',
            type: 'text',
            value: null
        },
        {
            prop: 'reductionVoucherAmount',
            name: '立减金额度',
            type: 'text',
            value: null
        },
        {
            prop: 'expiryPeriod',
            name: '过期期限',
            type: 'text',
            value: null
        },
        {
            prop: 'expiryPeriodType',
            name: '过期期限类型',
            type: 'text',
            value: null
        },
        {
            prop: 'remainderQuota',
            name: '剩余配额',
            type: 'text',
            value: null
        },
        {
            prop: 'issuedQuota',
            name: '已发放配额',
            type: 'text',
            value: null
        },
        {
            prop: 'receivedQuota',
            name: '已领取配额',
            type: 'text',
            value: null
        },
        {
            prop: 'usedQuota',
            name: '已使用配额',
            type: 'text',
            value: null
        },
        {
            prop: 'enabled',
            name: '是否启用',
            type: 'text',
            value: null
        },
        {
            prop: 'extInfo',
            name: '扩展信息',
            type: 'text',
            value: null
        },

    ])
    const tableProp = reactive(
        {
            name: "supplierList",
            propsList: [

                {
                    prop: 'supplierCode',
                    label: '供应商编码',
                    type: 'normal',
                    minWidth: '150px',
                    hidden: true,
                },
                {
                    prop: 'equityVoucherType',
                    label: '权益券类型',
                    type: 'normal',
                    minWidth: '90px',
                    hidden: true,
                },
                {
                    prop: 'equityVoucherCode',
                    label: '权益券编码',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    prop: 'equityVoucherName',
                    label: '权益券名称',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    prop: 'supplierEquityVoucherCode',
                    label: '供应商权益编码',
                    type: 'normal',
                    minWidth: '150px',
                    hidden: true,
                },
                {
                    prop: 'receiveType',
                    label: '领取类型',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'receiveValue',
                    label: '领取值',
                    type: 'normal',
                    minWidth: '80px',
                    hidden: true,
                },
                {
                    prop: 'reductionVoucherAmount',
                    label: '立减金额度',
                    type: 'normal',
                    minWidth: '90px',
                    hidden: true,
                },
                {
                    prop: 'expiryPeriod',
                    label: '过期期限',
                    type: 'normal',
                    minWidth: '80px',
                    hidden: true,
                },
                /*           {
                              prop: 'expiryPeriodType',
                              label: '过期期限类型',
                              type: 'normal',
                              minWidth: '80px',
                              hidden: true,
                          }, */
                {
                    prop: 'remainderQuota',
                    label: '剩余配额',
                    type: 'normal',
                    minWidth: '80px',
                    hidden: true,
                },
                {
                    prop: 'issuedQuota',
                    label: '已发放配额',
                    type: 'normal',
                    minWidth: '90px',
                    hidden: true,
                },
                {
                    prop: 'receivedQuota',
                    label: '已领取配额',
                    type: 'normal',
                    minWidth: '90px',
                    hidden: true,
                },
                {
                    prop: 'usedQuota',
                    label: '已使用配额',
                    type: 'normal',
                    minWidth: '90px',
                    hidden: true,
                },
                {
                    prop: 'enabled',
                    label: '是否启用',
                    type: 'normal',
                    minWidth: '80px',
                    hidden: true,
                },
                {
                    prop: 'extInfo',
                    label: '扩展信息',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    type: 'handle',
                    label: '操作',
                    minWidth: '200px',
                    btnType: ' link',
                    slotName: 'handle',
                    hidden: true,
                }
            ]
        })
    const listSearch = reactive({
        orders: ["created_time desc"],
        condition: {
            equityVoucherName: null,// 权益券名称
            supplierCode: null,// 供应商编码
            equityVoucherType: null,// 权益券类型
            equityVoucherCode: null,//  权益券编码
            supplierEquityVoucherCode: null,// 供应商权益编码
        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: []

    })

    const getDate1 = async (type: string) => {
        pageParams.loadIng = true

        if (type == 'reset') {
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            Object.keys(listSearch.condition).forEach((it: any) => {
                //@ts-ignore
                listSearch.condition[it] = null

            })
        }

        try {
            const res = await voucherSaveOrUpdatePagePost(listSearch)
            pageParams.loadIng = false
            if (res.code == 20000) {
                pageParams.tableData = res.data.records && res.data.records.map((item: any) => {

                    return {
                        ...item,
                        equityVoucherType: schemeConfigletailsType[item.equityVoucherType - 1]?.value,
                        receiveType: virtualType[item.receiveType - 1]?.value,
                        enabled: isShow[item.enabled],
                        expiryPeriod: item.expiryPeriod // + ' ' + expiryPeriodType[item.expiryPeriodType - 1] == 'undefined' ? expiryPeriodType[item.expiryPeriodType - 1]?.value : " "

                    }
                })
                pageParams.total = res.data.total
            }
        } catch (error) {
            console.log(error);
            pageParams.loadIng = false

        }finally{
            mittBus.emit('scrollUpdate')
        }
    }
    const detailBtn = async (row: any) => {
        try {
            const res = await equityDetailGet({ id: row.id })
            res.data.reductionVoucherType = res.data.reductionVoucherType + ''
            res.data.expiryPeriodType = res.data.expiryPeriodType + ''
            if (res.code != 20000) return
            detailData.value = detailData.value.map(item => {

                if (item.prop == "reductionVoucherType") {
                    return {
                        ...item,
                        value: reductionVoucherType[res.data[item.prop]]
                    }
                } else if (item.prop == "expiryPeriodType") {
                    return {
                        ...item,
                        value: expiryPeriodType[res.data[item.prop] - 1]?.value
                    }
                } else if (item.prop == "enabled") {
                    return {
                        ...item,
                        value: isShow[res.data[item.prop]]
                    }
                } else {
                    return {
                        ...item,
                        value: res.data[item.prop]
                    }
                }
            })
            detailRef.value.dialogBasic.height = 500
            detailRef.value?.show('edit')

        } catch (error) {

        }
    }

    const handPage = (params: any) => {
        listSearch.pageNum = params.pageNum
        listSearch.pageSize = params.pageSize
        getDate1('search')
    }
    const delBtn = async (row: any) => {
        const flag = await global.$myconfirm(`是否确定删除该数据？  ${row.equityVoucherName}`)
        if (flag) {
            const res = await voucherSaveOrUpdateDeleteDel({ id: row.id })
            console.log(res.data);
            if (res.code == 20000) {
                ElMessage({
                    type: "success",
                    message: "数据删除成功"
                })
                getDate1('search')
            } else {
                ElMessage({
                    type: "error",
                    message: "数据删除失败"
                })
            }
        }
    }
    const lookInventory = async (row: any) => {
        try {
            const res = await equityListDataByIdGet({ id: row.id })
            res.data = res.data.map((item: any) => {
                return {
                    ...item
                }
            })
            inventoryRef.value?.show('edit', res.data)
        } catch (error) {
            console.log(error);
        }
    }
    const AddEdit =async (type: string, row?: any) => {
        if (type == 'add') {
            addAndEditRef.value?.show('add')
        } else {
            try {
                const res = await voucherSaveOrUpdateDetailGet({id:row.id})
                if(res.code == 20000){
                    addAndEditRef.value?.show('edit',res.data)
                }
            } catch (error) {
                console.log(error);
                
            }
        }
    }
    return {
        detailData, tableProp, listSearch, pageParams, detailRef, addAndEditRef, inventoryRef,
        getDate1, handPage, delBtn, detailBtn, lookInventory, AddEdit
    }
}
    /* , {
        persist: {
            storage: sessionStorage,
            paths: ['detailData', 'tableProp'],
        }
    } */
)